package com.k2data.k2app.controller;

import com.k2data.k2app.mapper.SysMessageMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 系统消息通知
 * Created by clb on 17-3-27.
 */
@Api(description = "消息通知")
@RestController
@RequestMapping("/messages")
public class SysMessageController {
	private final SysMessageService sysMessageService ;
	private final SysMessageMapper sysMessageMapper;

	@Autowired
	public SysMessageController(SysMessageService sysMessageService, SysMessageMapper sysMessageMapper) {
		this.sysMessageService = sysMessageService;
		this.sysMessageMapper = sysMessageMapper;
	}

	@ApiOperation(value = "根据id获取")
	@ApiImplicitParam(name = "id", value = "the value of id", required = true, dataType = "String", paramType = "path")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> get(@PathVariable("id") String id) throws Exception {
		return ResponseHelper.success(sysMessageMapper.get(Long.parseLong(id)));
	}

	@ApiOperation(value = "根据消息类型获取")
	@ApiImplicitParam(name = "type", value = "the value of 'type", required = true, dataType = "String", paramType = "query")
	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> getByType(@RequestParam("type") String type) throws Exception {
		return ResponseHelper.success(sysMessageService.getByType(type));
	}

	@ApiOperation(value = "忽略消息")
	@ApiImplicitParam(name = "id", value = "the value of id", required = true, dataType = "Long", paramType = "path")
	@PutMapping(value = "/{id}/deleted")
	public ResponseEntity<CommonResultResponse> deleted(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(sysMessageMapper.updateFlag(id));
	}

}
